import axios from 'axios';
import jquery from 'jquery';
import Qs from 'qs';

let base = '';


export const getRoutes = params => { return axios.get(`${base}/routes`, { params: params }); };

let baseUrl = 'http://127.0.0.1:9000/'

export const modelListJSONSync = () => { 
	console.info( baseUrl+"ptcrud/modelListJSON" ) ;
	var result ; 
	jquery.ajax({
		async:false ,
		url: baseUrl+"ptcrud/modelListJSON",
	 	dataType: 'json',
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};

export const modelListJSON = params => { 
	return axios.get( baseUrl+"ptcrud/modelListJSON", { params:params.params}).then(res=>{
		return res.data ;
	})
	.catch(function (error) {
		console.info(error);
	});
};

export const allJSONSync = params => { 
	 
	var result ; 
	jquery.ajax({
		async:false ,
		type:"GET",
		data:params.params,
		url: baseUrl+params.type+"/allJSON",
	 	dataType: 'json',
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};

export const allJSON = params => { 
	return axios.get( baseUrl+params.type+"/allJSON", { params:params.params}).then(res=>{
		return res.data ;
	})
	.catch(function (error) {
		console.info(error);
	});
};


export const typeInfoJsonSync = params => { 
	 
	var result ; 
	jquery.ajax({
		async:false ,
		type:"GET",
		data:params.params,
		url: baseUrl+params.type+"/typeInfoJson",
	 	dataType: 'json',
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};


export const typeInfoJson = params => { 
	return axios.get( baseUrl+params.type+"/typeInfoJson", { params:params.params}).then(res=>{
		return res.data ;
	})
	.catch(function (error) {
		console.info(error);
	});
};


export const listJsonSync = params => { 
	 
	var result ; 
	jquery.ajax({
		async:false ,
		type:"GET",
		data:params.params,
		url: baseUrl+params.type+"/listJson",
	 	dataType: 'json',
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};


export const listJson = params => { 

	return axios.get( baseUrl+params.type+"/listJson", { params:params.params}).then(res=>{
		return res.data ;
	})
	.catch(function (error) {
		console.info(error);
	});
};

export const createJsonSync = params => { 
	 
	var result ; 
	jquery.ajax({
		async:false ,
		type:"GET",
		data:params.params,
		url: baseUrl+params.type+"/createJson",
	 	dataType: 'json',
	 	traditional: true ,	// 会将url中的数组[]去掉
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};

export const createJson = params => { 
	
	return axios.get( baseUrl+params.type+"/createJson", 
						{ params:params.params,
							paramsSerializer: function(params) {
    							return Qs.stringify(params, {arrayFormat: 'brackets'}) // 会将url中的数组[]去掉
  							},
						}).then(res=>{
							return res.data ;
						})
						.catch(function (error) {
							console.info(error);
						});
};



export const deleteJSONSync = params => { 
	 
	var result ; 
	jquery.ajax({
		async:false ,
		type:"GET",
		data:params.params,
		url: baseUrl+params.type+"/deleteJSON",
	 	dataType: 'json',
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};

export const deleteJSON = params => { 
	
	return axios.get( baseUrl+params.type+"/deleteJSON", { params:params.params}).then(res=>{
		return res.data ;
	})
	.catch(function (error) {
		console.info(error);
	});
	
};

export const batchDeleteJSONSync = params => { 
	 
	var result ; 
	jquery.ajax({
		async:false ,
		type:"GET",
		data:params.params,
		url: baseUrl+params.type+"/batchDeleteJSON",
	 	dataType: 'json',
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};

export const batchDeleteJSON = params => { 
	
	return axios.get( baseUrl+params.type+"/batchDeleteJSON", { params:params.params}).then(res=>{
		return res.data ;
	})
	.catch(function (error) {
		console.info(error);
	});
	
};



export const showJSONSync = params => { 
	 
	var result ; 
	jquery.ajax({
		async:false ,
		type:"GET",
		data:params.params,
		url: baseUrl+params.type+"/showJSON",
	 	dataType: 'json',
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};

export const showJSON = params => { 
	
	return axios.get( baseUrl+params.type+"/showJSON", { params:params.params}).then(res=>{
		return res.data ;
	})
	.catch(function (error) {
		console.info(error);
	});
	
};


export const updateJSONSync = params => { 
	 
	var result ; 
	jquery.ajax({
		async:false ,
		type:"GET",
		data:params.params,
		url: baseUrl+params.type+"/updateJSON",
	 	dataType: 'json',
	 	traditional: true ,	// 会将url中的数组[]去掉
		success: function (res) {  
	        result = res;  
	    },  
	    error: function (msg) {  
	      result = msg;  
	    }  
	});
	
	return result;
};

export const updateJSON = params => { 
	
	return axios.get( baseUrl+params.type+"/updateJSON", 
					{ params:params.params,
						paramsSerializer: function(params) {
												
							var str = Qs.stringify(params, {arrayFormat: 'repeat'}) ;
							console.info( "updateJson->"+ str ) ;
							return str; // 会将url中的数组[]去掉
						},
					}).then(res=>{
						return res.data ;
					})
					.catch(function (error) {
						console.info(error);
					});
};

